#coding=utf-8
#Boa:Dialog:DlgAddCustomerLog

import wx

from ctrlAider import *
from product import *
from config import *

def create(parent):
    return DlgAddCustomerLog(parent)

[wxID_DLGADDCUSTOMERLOG, wxID_DLGADDCUSTOMERLOGBUTTON_SAVE, 
 wxID_DLGADDCUSTOMERLOGCHECKBOX_DELIVERY, 
 wxID_DLGADDCUSTOMERLOGCOMBOBOX_REPORTER, 
 wxID_DLGADDCUSTOMERLOGDATEPICKERCTRL1, wxID_DLGADDCUSTOMERLOGSTATICTEXT1, 
 wxID_DLGADDCUSTOMERLOGTCCONTENT, wxID_DLGADDCUSTOMERLOGTCHOUR, 
 wxID_DLGADDCUSTOMERLOGTCMIN, 
] = [wx.NewId() for _init_ctrls in range(9)]

class DlgAddCustomerLog(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGADDCUSTOMERLOG,
              name=u'DlgAddCustomerLog', parent=prnt, pos=wx.Point(421, 183),
              size=wx.Size(504, 261), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u5ba2\u60c5\u7f16\u8f91')
        self.SetClientSize(wx.Size(488, 222))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.comboBox_reporter = wx.ComboBox(choices=[],
              id=wxID_DLGADDCUSTOMERLOGCOMBOBOX_REPORTER,
              name=u'comboBox_reporter', parent=self, pos=wx.Point(328, 32),
              size=wx.Size(110, 24), style=0, value=u'')
        self.comboBox_reporter.SetLabel(u'')

        self.TCContent = wx.TextCtrl(id=wxID_DLGADDCUSTOMERLOGTCCONTENT,
              name=u'TCContent', parent=self, pos=wx.Point(56, 72),
              size=wx.Size(384, 22), style=0, value=u'')

        self.button_save = wx.Button(id=wxID_DLGADDCUSTOMERLOGBUTTON_SAVE,
              label=u'\u4fdd\u5b58', name=u'button_save', parent=self,
              pos=wx.Point(200, 152), size=wx.Size(88, 32), style=0)
        self.button_save.Center(wx.HORIZONTAL)
        self.button_save.Bind(wx.EVT_BUTTON, self.OnButton_saveButton,
              id=wxID_DLGADDCUSTOMERLOGBUTTON_SAVE)

        self.checkBox_delivery = wx.CheckBox(id=wxID_DLGADDCUSTOMERLOGCHECKBOX_DELIVERY,
              label=u'\u9001\u8d27\u62a5\u5907', name=u'checkBox_delivery',
              parent=self, pos=wx.Point(56, 104), size=wx.Size(264, 24),
              style=0)
        self.checkBox_delivery.SetValue(False)
        self.checkBox_delivery.Show(False)
        self.checkBox_delivery.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox_deliveryCheckbox,
              id=wxID_DLGADDCUSTOMERLOGCHECKBOX_DELIVERY)

        self.TCHour = wx.TextCtrl(id=wxID_DLGADDCUSTOMERLOGTCHOUR,
              name=u'TCHour', parent=self, pos=wx.Point(184, 32),
              size=wx.Size(48, 22), style=0, value=u'')

        self.TCMin = wx.TextCtrl(id=wxID_DLGADDCUSTOMERLOGTCMIN, name=u'TCMin',
              parent=self, pos=wx.Point(256, 32), size=wx.Size(48, 22), style=0,
              value=u'')

        self.staticText1 = wx.StaticText(id=wxID_DLGADDCUSTOMERLOGSTATICTEXT1,
              label=u'\uff1a', name='staticText1', parent=self,
              pos=wx.Point(240, 36), size=wx.Size(12, 14), style=0)

        self.datePickerCtrl1 = wx.DatePickerCtrl(id=wxID_DLGADDCUSTOMERLOGDATEPICKERCTRL1,
              name='datePickerCtrl1', parent=self, pos=wx.Point(56, 32),
              size=wx.Size(112, 22), style=wx.DP_SHOWCENTURY)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.datePickerCtrl1.SetValue(wx.DateTime.Today())
        now = wx.DateTime.Now()
        self.TCHour.SetValue(now.Format("%H"))#hour
        self.TCMin.SetValue(now.Format("%M"))#minute
        
        self.cbHandler = MyComboBoxDecorator(self.comboBox_reporter, CDataStaff, CDataStaff.COL_NAME, only_enable_staff)
        self.cbHandler.refresh()
        self.cbHandler.SetValue(Cfg.getCurUser().getValue(CDataStaff.COL_NAME))
        
        self.TCContent.SetFocus()

    def setData(self, cust_id, delvery_id=None):
        
        self.m_custId = cust_id
        self.m_delvery_id = delvery_id
            
        self.checkBox_delivery.SetValue(self.m_delvery_id is not None and self.m_delvery_id != "")
        self.checkBox_delivery.Show(self.m_delvery_id is not None and self.m_delvery_id != "")
        if (self.m_delvery_id is not None and self.m_delvery_id != ""):
            self.TCContent.SetValue(CDataCustomer.GetLogContent(self.m_custId, self.m_delvery_id))
        
    def OnButton_saveButton(self, event):

        if (self.TCContent.GetValue().strip() == ""):
          return
          
        report_time = "%s %02d:%02d:00" \
            %(self.datePickerCtrl1.GetValue().Format('%Y-%m-%d')
            ,int(self.TCHour.GetValue())
            ,int(self.TCMin.GetValue()))
        CDataCustomer.AddLog(self.m_custId
            ,self.cbHandler.getSelectObj().getValue(CDataStaff.COL_ID)
            ,self.cbHandler.getSelectObj().getValue(CDataStaff.COL_NAME)
            ,self.TCContent.GetValue()
            ,self.m_delvery_id if self.checkBox_delivery.GetValue() else None
            ,report_time)
        self.EndModal(wx.ID_OK)

    def OnCheckBox_deliveryCheckbox(self, event):
        if (self.checkBox_delivery.GetValue()):
            self.TCContent.SetValue(CDataCustomer.GetLogContent(self.m_custId, self.m_delvery_id))
        else:
            self.TCContent.SetValue("")
